home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / CENVIW2.ZIP / INSTALL.CMM < prev    next >
Text File  |  1993-08-09  |  11KB  |  303 lines

  1. /*************************************************************************
  2.  ***                            Install.cmm                            ***
  3.  *** This is the installation program to set up CEnvi unregistered     ***
  4.  *** shareware on your computer.  Before running this program, CEnvi   ***
  5.  *** must be copied to a hard disk directory.  This Install.cmm        ***
  6.  *** program is identical for DOS, OS/2, and Windows version of CEnvi  ***
  7.  *** and this program is itself an example for how to use CEnvi        ***
  8.  *************************************************************************/
  9.  
  10.  
  11. main(argc,argv)
  12. {
  13.    // There should be no input args.  If there are then the user needs help.
  14.    ScreenClear();
  15.    if ( argc != 1 )
  16.       GiveInstructionsAndExit();
  17.  
  18.    // Get the current directory.  The GetInstallDirectory() function assures
  19.    // that it is not a floppy.
  20.    InstallDirectory = GetInstallDirectory(argv[0]);
  21.  
  22.    if defined( _DOS_ )
  23.       DosOrOS2Install(InstallDirectory,"C:\\AUTOEXEC.BAT","C:\\AUTOEXEC.BAK");
  24.    else if defined( _OS2_)
  25.       DosOrOS2Install(InstallDirectory,"C:\\CONFIG.SYS","C:\\CONFIG.BAK");
  26.    else {
  27.       assert( defined(_WINDOWS_) );
  28.       WindowsInstall(InstallDirectory);
  29.       printf("\nPress any key to exit...");
  30.       getch();
  31.    }
  32.    return(EXIT_SUCCESS);
  33. }
  34.  
  35.  
  36. GiveInstructionsAndExit()  // Show some info about using this program
  37. {                          // and then exit this program. Do not return.
  38.    printf("Install.cmm - CEnvi installation procedure.  Execute with no parameters\n");
  39.    printf("              from the directory that contains CEnvi.exe and also contains\n");
  40.    printf("              the *.cmm and other sample CEnvi files, including Install.cmm\n");
  41.    if defined( _WINDOWS_ ) {
  42.       printf("\nPress any key to exit...");
  43.       getch();
  44.    }
  45.    exit(EXIT_FAILURE);
  46. }
  47.  
  48.  
  49. GetInstallDirectory(Executable)   // return current dir that is not on a floppy
  50. {
  51.    // current source directory from the name of the executable
  52.    CurDir = SplitFileName(FullPath(Executable)).dir;
  53.    assert( CurDir != NULL  &&  CurDir[0] != 0 );
  54.  
  55.    // check that current directory is not floppy A: or B:
  56.    if ( CurDir[0] == 'A'  ||  CurDir[0] == 'B' ) {
  57.       printf("\nCannot install CEnvi from a floppy.\n\a\n");
  58.       GiveInstructionsAndExit();
  59.    }
  60.  
  61.    // Have found the directory Install.cmm is in, and so check that
  62.    // CEnvi.exe is also in the same directory.
  63.    if ( NULL == Directory(strcat(strcpy(temp,CurDir),"CEnvi.exe")) ) {
  64.       printf("\nThis installation assumes that CEnvi.exe is in the same directory as\n");
  65.       printf("Install.cmm.  Could not find CEnvi.exe in the Install.cmm directory.\n\a\n");
  66.       GiveInstructionsAndExit();
  67.    }
  68.  
  69.    // remove extra backslash if at end of name (i.e., not root directory)
  70.    if ( strcmp(CurDir+1,":\\") )
  71.       CurDir[strlen(CurDir)-1] = 0;
  72.  
  73.    // return the result
  74.    return(CurDir);
  75. }
  76.  
  77.  
  78. Fatal(Format)  // like printf() but also beeps and exits program
  79. {
  80.    va_start(parameters,Format);
  81.    printf("\a\n");  // beep
  82.    vprintf(Format,parameters);
  83.    exit(EXIT_FAILURE);
  84. }
  85.  
  86.  
  87. GetYesOrNo()   // prompt for Yes or No, and return entered boolean TRUE
  88. {              // if YES else FALSE if NO.
  89.    printf(" (Y/N) ");
  90.    while( TRUE ) { // forever
  91.       key = toupper(getch());
  92.       if ( key != 'Y' && key != 'N' )
  93.          printf("\a");  // beep
  94.       else
  95.          break;
  96.    }
  97.    printf("%c\n",key);
  98.    return( key == 'Y' );
  99. }
  100.  
  101.  
  102. CopyFile(Source,Destination) // copy file from source to destination
  103. {
  104.    // open source and destination files
  105.    src = fopen(Source,"rb");
  106.    if ( src == NULL )
  107.       Fatal("Could not open source file \"%s\" for reading.",Source);
  108.    dest = fopen(Destination,"wb");
  109.    if ( dest == NULL )
  110.       Fatal("Could not open file \"%s\" for writing.",Destination);
  111.  
  112.    // read chunks from source, and copy to destination
  113.    #define  COPY_CHUNK_SIZE   500
  114.    while ( 0 != (size = fread(buf,COPY_CHUNK_SIZE,src)) ) {
  115.       fwrite(buf,size,dest);
  116.    }
  117.  
  118.    // close the files
  119.    fclose(dest);
  120.    fclose(src);
  121. }
  122.  
  123.  
  124. SkipWhitespace(str)
  125. {
  126.    while( isspace(str[0]) )
  127.       str++;
  128. }
  129.  
  130.  
  131. AlreadyInPath(Dir,Path) // search through path for this Dir, return True if found, else False
  132. {
  133.    len = strlen(Dir)
  134.    p = Path;
  135.    do {
  136.       if ( 0 == strnicmp(p,Dir,len)  && (p[len]==0 || p[len]==';') )
  137.          return(True)
  138.       p = strchr(p,';')
  139.    } while( p++ != NULL )
  140.    return(False)
  141. }
  142.  
  143.  
  144. AlterEVarLine(text,EVar,Dir)
  145.    // If this text is a line setting the EVar environment variable, and if
  146.    // Dir is not already in it, then add Dir.  Return TRUE if this is
  147.    // a line for EVar, and False if it is not.
  148. {
  149.    FoundEVar = FALSE; // assume this is not the line
  150.    // determine if text is of the type "EVAR=" or "set EVAR="
  151.    SkipWhitespace(c = text);
  152.    // if the next statement is "SET" then skip it
  153.    if ( !strncmpi(c,"SET",3) ) {
  154.       // Skip beyond the SET statement
  155.       c += 3;
  156.       SkipWhitespace(c);
  157.    }
  158.    // test if this next part is the variable name
  159.    EVarLen = strlen(EVar);
  160.    if ( !strncmpi(c,EVar,EVarLen) ) {
  161.       c += EVarLen;
  162.       if ( isspace(c[0])  ||  c[0] == '=' ) {
  163.          // THIS IS IT.  This line describes the EVar.
  164.          SkipWhitespace(c);
  165.          if ( c[0] == '=' ) {
  166.             c++;
  167.             SkipWhitespace(c);
  168.          }
  169.          FoundEVar = TRUE;
  170.          // If Dir is not already in this line then add Dir at the end.
  171.          // Check each dir as value between semicolons (;)
  172.          SkipWhitespace(c);
  173.          if ( !AlreadyInPath(Dir,c) ) {
  174.             // add this to the end of the existing path
  175.             if ( c[strlen(c)-1] != ';' )
  176.                strcat(c,";");
  177.             strcat(c,Dir);
  178.          }
  179.       }
  180.    }
  181.    return(FoundEVar);
  182. }
  183.  
  184. DosOrOS2Install(Dir,FileName,BackupFileName)
  185. {
  186.    // append the PATH statement so that it contains this directory, and
  187.    // also add the CMMPATH environment variable.  But give user a choice
  188.    // of whether to do this first; if they choose not to then tell them
  189.    // what they must do by hand.
  190.    printf("\nIf you choose, install will add the directory:\n\n");
  191.    printf("    \"%s\"\n\n",Dir);
  192.    printf("to the PATH environment variable in your %s file.  Install will\n",FileName);
  193.    printf("also add this directory to the CMMPATH environment variable in that\n");
  194.    printf("file.  If you select to make these changes to %s, then\n",FileName);
  195.    printf("install will backup the current version of %s to %s.\n",FileName,BackupFileName);
  196.    printf("\n\nShould these changes be made to %s?",FileName);
  197.    if ( GetYesOrNo() ) {
  198.       printf("Making changes to %s...",FileName);
  199.       // user chose to make automatic changes.  Do so now
  200.       CopyFile(FileName,BackupFileName);
  201.  
  202.       // will now read the backup file, and if any PATH or CMMPATH line is
  203.       // encountered then will alter it, else just copy line exactly.
  204.       src = fopen(BackupFileName,"rt");
  205.       if ( src == NULL )
  206.          Fatal("Could not open source file \"%s\" for reading.",BackupFileName);
  207.       dest = fopen(FileName,"wt");
  208.       if ( dest == NULL )
  209.          Fatal("Could not open source file \"%s\" for reading.",BackupFileName);
  210.       SetPath = SetCmmPath = False;
  211.       while ( NULL != (line = fgets(src)) ) {
  212.          // remove the pesky newline if there is one
  213.          if ( PeskyNewLine = (line[strlen(line)-1] == '\n') )
  214.             line[strlen(line)-1] = 0;
  215.          if ( AlterEVarLine(line,"PATH",Dir) )
  216.             SetPath = True;
  217.          if ( AlterEVarLine(line,"CMMPATH",Dir) )
  218.             SetCmmPath = True;
  219.          fputs(line,dest);
  220.          if ( PeskyNewLine )
  221.             fputs("\n",dest);
  222.       }
  223.       fclose(src);
  224.       // if haven't already written PATH or CMMPATH, then do so now
  225.       if ( !SetPath )
  226.          fprintf(dest,"\nSET PATH=%s\n",Dir);
  227.       if ( !SetCmmPath )
  228.          fprintf(dest,"\nSET CMMPATH=%s\n",Dir);
  229.       fclose(dest);
  230.       printf("\n%s has been altered.\n",FileName);
  231.       printf("Changes will take effect a